package com.ashley.stack;


/**
 * @Author ashley
 * @Date 2019/9/10 11:16
 */
public class ArrayStack {
    private int size;//-----????????????????
    private int top = -1;//-----?????????????�?
    private int[] arr;//-----??????????

    public ArrayStack() {
        arr = new int[3];//-----??????????????3
    }

    //???????????????????
    public ArrayStack(int size) {
        if (size > 0) {
            this.size = size;
            arr = new int[size];
        }
        else throw new RuntimeException("????????????");
    }

    //???
    public void push(int data) {
        if (top >= size - 1) {
            System.out.println("???????????????????????");
            return;
        }
        arr[++top] = data;
    }

    //???
    public int pop() {
        if (top < 0) {
            throw new RuntimeException("????????????????????");
        } else {
            int temp = arr[top];
            arr[top] = 0;
            top--;
            return temp;
        }
    }

    //????????????????
    public int peek() {
        if (top < 0) {
            throw new RuntimeException("?????");
        } else {
            return arr[top];
        }
    }

    //??????????
    public boolean isEmpty() {
        return top == -1;
    }

    //??????
    public boolean isFull() {
        return top == size - 1;
    }

    //?????????????
    public int sum() {
        int count = top + 1;
        return count;
    }
}



